<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./js/vue.js"></script>
    <script src="./js/axios.js"></script>
    <script src="./js/lodash.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            list-style: none;
        }

        .tr {
            display: flex;
        }

        .tr .td {
            flex: 1;
            text-align: center;
            line-height: 40px;
            background-color: black;
            color: white;
        }
    </style>
</head>

<body>
    <div id="app">
        <div>
            <div>用户名 <input v-model="username" /></div>
            <div>年龄 <input v-model="userage" /></div>
            <button @click="add"> 新增</button>
        </div>
        <hr />
        <div>
            用户名 <input v-model="key" />
        </div>
        <div class="tr">
            <div class="td">编号</div>
            <div class="td">姓名</div>
            <div class="td">年龄</div>
            <div class="td">操作</div>
            <div class="td">编辑</div>
        </div>
        <div class="tr" v-for="item  in list">
            <div class="td">{{item.id}}</div>
            <div class="td">
                <span v-if="!item.isEdit">{{item.username}}</span>
                <span v-else>
                    <input v-model="item.username"/>
                </span>
            </div>
            <div class="td">
                <span v-if="!item.isEdit">{{item.userage}}</span>
                <span v-else>
                    <input v-model="item.userage"/>
                </span>
            </div>
            <div class="td" @click="delById(item.id)">删除</div>
            <div class="td">
                <span v-show="!item.isEdit" @click="eidtInfo(item)">编辑</span>
                <span v-show="item.isEdit">
                    <span @click="confirm(item)">确定</span>
                    <span @click="cancel(item)">取消</span>
                </span>
            </div>
        </div>
    </div>

</body>
<script>
    new Vue({
        el: "#app",
        data: {
            key: "",//搜索的关键字
            username: "",
            userage: "",
            list: [],
            timer: null,
        },
        watch: {//监听
            key() {
                this.debouncedSearch();
            }
        },
        async created() {
            this.debouncedSearch = _.debounce(this.search, 500);
            this.debouncedSearch();
        },
        methods: {
          async  confirm(item){
                item.isEdit = false;

                await  axios({
                    method:"patch",
                    url:"http://localhost:2345/userinfo/"+item.id,
                    data:{
                        username:item.username,
                        userage:item.userage
                    }
                })

                item.defaultName  = item.username;
                item.defaultAge  = item.userage;


            },
            cancel(item){
               item.isEdit = false;
               item.username  = item.defaultName;
               item.userage  = item.defaultAge;
            },

            eidtInfo(item){
                item.isEdit = true;
            },
          async  search() {
                let { data: list } = await axios({
                    method: "get",
                    url: "http://localhost:2345/userinfo",
                    params: {
                        "username_like": this.key
                    }
                })
                //给每条数据 新增一个状态
                this.list = list.map(item=>{
                    item.isEdit = false;//控制是否点击编辑按钮
                    item.defaultName = item.username;
                    item.defaultAge = item.userage;
                    return item;
                });

            },
            async delById(id) {
                await axios({
                    method: "delete",
                    url: "http://localhost:2345/userinfo/" + id
                })
                this.list = this.list.filter(item => item.id !== id);
            },
            async add() {
                let { data } = await axios({
                    method: "post",
                    url: "http://localhost:2345/userinfo",
                    data: {
                        username: this.username,
                        userage: this.userage
                    }
                })
                //填充数据
                data.isEdit =false;
                data.defaultName = data.username;
                data.defaultAge  = data.userage;
                this.list.push(data);

            }
        }
    })

</script>

</html>